<template>
  <!--  <a-drawer
    title="新增用户"
    :maskClosable="false"
    width="650"
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="userAddVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;"
  >-->
  <modal
    ref="testModal"
    :visible="userAddVisiable"
    title="新增用户"
    :okText="保存"
    @cancelHandler="onClose"
    @okHandler="handleSubmit"
  >
    <template slot="content">
      <a-form :form="form">
        <a-form-item label="昵称" v-bind="formItemLayout">
          <a-input v-decorator="['username', { rules: [{ required: true, message: '昵称不能为空' }] }]" />
        </a-form-item>
        <a-form-item label="账号" v-bind="formItemLayout">
          <a-input placeholder = '一般是手机号码' v-decorator="['usercode', { rules: [{ required: true, message: '账号不能为空' }] }]" />
        </a-form-item>
        <a-form-item label="手机" v-bind="formItemLayout">
          <a-input placeholder = '一般是手机号码' v-decorator="['mobile', { rules: [{ required: false, message: '账号不能为空' }] }]" />
        </a-form-item>
        <a-form-item label="密码" v-bind="formItemLayout">
          <a-input v-decorator="['password', { rules: [{required: true, message: '密码不能为空' }] }]" />
        </a-form-item>
        <a-form-item label="角色" v-bind="formItemLayout">
          <a-select
            mode="multiple"
            :allowClear="true"
            style="width: 100%"
            v-decorator="['roleId', { rules: [{ required: true, message: '请选择角色' }] }]"
          >
            <a-select-option v-for="r in roleData" :key="r.roleId">{{ r.roleName }}</a-select-option>
          </a-select>
        </a-form-item>        
        <a-form-item label="邮箱" v-bind="formItemLayout" :validateStatus="validateStatus" :help="help">
          <a-input readOnly v-decorator="['email', { rules: [{ required: false, message: '邮箱不能为空' }] }]" />
        </a-form-item>
        <a-form-item label="性别" v-bind="formItemLayout" :validateStatus="validateStatus" :help="help">
          <!--<a-input readOnly v-decorator="['ssex', { rules: [{ required: false, message: '性别不能为空' }] }]" />-->
          <a-select
            style="width: 100%"
            defaultValue = "3"
            v-decorator="['ssex', { rules: [{ required: false, message: '性别不能为空' }] }]"
          >
            <a-select-option key="1" value="1">男</a-select-option>
            <a-select-option key="2" value="2">女</a-select-option>
            <a-select-option key="3" value="3">保密</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </template>
  </modal>
</template>
<script>
import modal from '~/tool/modal';
const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 14 }
};
export default {
  name: 'UserAdd',
  components: { modal },
  props: {
    userAddVisiable: {
      default: false
    }
  },
  data() {
    return {
      user: {
        username: ''
      },
      okText: '验证',
      loading: false,
      roleData: [],
      formItemLayout,
      form: this.$form.createForm(this),
      validateStatus: '',
      help: ''
    };
  },
  methods: {
    reset() {
      this.validateStatus = '';
      this.help = '';
      this.user.username = '';
      this.loading = false;
      this.form.resetFields();
    },
    onClose() {
      this.$confirm({
        title: '确定放弃编辑吗？',
        content: '',
        okText: '确定',
        cancelText: '取消',
        onOk: this.onCloseConfirm
      });
    },
    onCloseConfirm() {
      this.reset();
      this.$emit('close');
    },
    handleSubmit() {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true; 
          const params = this.form.getFieldsValue();
          this.$post('user', {
            ...params
          })
            .then(r => {
              if (r.data.code === 200) {
                this.reset();
                this.$emit('success');
              } else {
                this.$message.error(r.data.message);
              }
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    }
  },
  watch: {
    userAddVisiable() {
      if (this.userAddVisiable) {
        this.$get('role').then(r => {
          this.roleData = r.data.data.rows;
        });
      }
    }
  }
};
</script>
